home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / DesktopDoubler / Common / Headers / LList.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-22  |  2.0 KB  |  165 lines  |  [TEXT/CWIE]

  1. #ifndef _LIST_
  2. #define _LIST_
  3.  
  4.  
  5. template <class T>
  6. class LList
  7. {
  8. public:
  9.     T        *fListHead;
  10.     T        *fListTail;
  11.     SInt32    fItemCount;
  12.     
  13.     inline LList(void) { fListHead = fListTail = NULL; fItemCount = 0; };
  14.     inline ~LList(void) { };
  15.     
  16.     void Push(T *elem);
  17.     inline T *Pop(void);
  18.     
  19.     inline void Append(T *elem);
  20.     void Insert(T *after,T *elem);
  21.     void Delete(T *elem);
  22.     
  23.     T *GetNthElement(UInt32 n);
  24.     
  25.     inline T *GetFirst(void) { return fListHead; };
  26.     inline SInt32 isEmpty(void) { return (fItemCount == 0); };
  27. };
  28.  
  29.  
  30.  
  31.  
  32.  
  33. template <class T> void LList<T>::Push(T *elem)
  34. {
  35.     if (fListTail == NULL)
  36.         fListTail = elem;
  37.     
  38.     elem->next = fListHead;
  39.     fListHead = elem;
  40.     fItemCount += 1;
  41. }
  42.  
  43.  
  44.  
  45.  
  46.  
  47. template <class T> T *LList<T>::Pop(void)
  48. {
  49.     T    *elem;
  50.     
  51.     
  52.     elem = fListHead;
  53.     if (elem != NULL)
  54.     {
  55.         fListHead = elem->next;
  56.         
  57.         if (fListTail == elem)
  58.             fListTail = fListHead;
  59.         
  60.         fItemCount -= 1;
  61.     }
  62.     
  63.     return elem;
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70. template <class T> void LList<T>::Append(T *elem)
  71. {
  72.     if (fListTail != NULL)
  73.         fListTail->next = elem;
  74.     else
  75.         fListHead = elem;
  76.     
  77.     elem->next = NULL;
  78.     fListTail = elem;
  79.     fItemCount += 1;
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86. template <class T> void LList<T>::Insert(T *after,T *elem)
  87. {
  88.     if (after == NULL)
  89.     {
  90.         Append(elem);
  91.         return;
  92.     }
  93.     
  94.     elem->next = after->next;
  95.     after->next = elem;
  96.     
  97.     if (fListTail == after)
  98.         fListTail = elem;
  99.     
  100.     fItemCount += 1;
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107. template <class T> void LList<T>::Delete(T *elem)
  108. {
  109.     T    *walk,*next;
  110.     
  111.     
  112.     if (elem == fListHead)
  113.     {
  114.         if (elem == fListTail)
  115.         {
  116.             fListHead = NULL;
  117.             fListTail = NULL;
  118.         }
  119.         else
  120.             fListHead = fListHead->next;
  121.         
  122.         fItemCount -= 1;
  123.     }
  124.     else
  125.     {
  126.         walk = fListHead;
  127.         while((next = walk->next) != NULL)
  128.         {
  129.             if (next == elem)
  130.             {
  131.                 walk->next = elem->next;
  132.                 if (elem == fListTail)
  133.                     fListTail = walk;
  134.                 
  135.                 fItemCount -= 1;
  136.                 return;
  137.             }
  138.             
  139.             walk = next;
  140.         }
  141.     }
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. template <class T> T *LList<T>::GetNthElement(UInt32 n)
  149. {
  150.     T    *elem;
  151.     
  152.     
  153.     elem = fListHead;
  154.     while((elem != NULL) && (n > 0))
  155.     {
  156.         elem = elem->next;
  157.         n -= 1;
  158.     }
  159.     
  160.     return elem;
  161. }
  162.  
  163.  
  164. #endif /* _LIST_ */
  165.